package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.BlogMapper;
import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.entity.Blog;
import cn.tedu.weibo.pojo.entity.User;

import cn.tedu.weibo.pojo.vo.BlogIndexVO;
import cn.tedu.weibo.pojo.vo.WeiboDetailVo;
import cn.tedu.weibo.result.JSonResult;
import cn.tedu.weibo.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 处理和博客相关的操作
 */
@RestController
@RequestMapping("/v1/weibo")
@Api(tags = "博客模块")
@Validated
public class BlogController {
    @Autowired
  private BlogMapper blogMapper;
    @Autowired //redis
    private RedisTemplate<String,Object> redisTemplate;
    @PostMapping("/insert")
    @ApiOperation("发表微博") //ApiOperation 具体的操作
    public JSonResult addBlog(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        System.out.println(weiboDTO);
        //验证是否登录
        User user=(User) session.getAttribute("user");
        if (user==null){
            //没有登录
            return new JSonResult(Status.NOT_LOGIN);
        }
        Blog blog =new Blog();
        blog.setContent(weiboDTO.getContent());
        blog.setUserId(user.getId());
        blog.setCreated(new Date());
        blogMapper.insert(blog);
        return JSonResult.success();
    }

    @GetMapping("/selectIndex")
    @ApiOperation("首页微博列表")
//    public List<BlogIndexVO> findIndexBlog(){
//        return blogMapper.findAll();
//    }
    public JSonResult findIndexBlog(){
         /*
            1:首先从redis中获取"weiboList"对应的List集合
            2:返回值为NULL(还没有放入过redis)
              2.1:通过Mapper从数据库中查询数据
              2.2:将查询的集合存入到redis中
            3:将该集合返回给客户端
         */
        ValueOperations ops= redisTemplate.opsForValue();
        List<BlogIndexVO> list=(List<BlogIndexVO>)ops.get("weiboList");
        if (list==null){
            list=blogMapper.findAll();//去数据库中查数据
            ops.set("weiboList",list);//存入redis
        }
        return JSonResult.success(list);
        //return JSonResult.success(blogMapper.findAll());
    }

    @GetMapping("/selectById")
    @ApiOperation("查看微博详情")
//    public WeiboDetailVo findById(Integer id){
//        return  blogMapper.findById(id);
    /**
     * 当我们方法的参数不是一个DTO，而是一个值
     * 那么可以使用@ApiImplicitParam在文档网页上说明该参数
     * 其中:
     * name:指定参数的名字(为方法的哪个参数添加说明)
     * value:参数的解释
     * required:是否必须传该参数
     * example:实例
     * dataType:数据类型是什么
     */
    @ApiImplicitParam(name="id",value="博客ID",required=true,example="1",dataType="int" )
    public JSonResult findById(@Range(min = 1,max=999,message = "微博ID只能在1-999之间") @RequestParam Integer id) {
        //@Range 异常@Validated要在类上面加

//        if(id<1){
//            //优先找同类型异常没有就找超类型异常 IllegalArgumentException继承RuntimeException
//            throw new  IllegalArgumentException("ID不能小于1");
//        }
    return  JSonResult.success(blogMapper.findById(id));

    }

    @GetMapping("/findByIdAndNickname")
    @ApiOperation("查询博客ID和作者昵称")
    //注意里面要用数组形式
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "博客ID",required = true,example = "1",dataType = "int"),
            @ApiImplicitParam(name = "nickname",value = "作者昵称",required = true,example = "啊三",dataType = "String")
    }
    )
    public JSonResult findByIdAndNickname(Integer id,String nickname){
        //不具体实现了，主要介绍方法上的文档注解
        return JSonResult.success();
    }

}
